package com.xyz.musicplayer;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import beans.Song;
import beans.User;
import utils.SongList;

public class FragMusicList extends Fragment {

    //类变量
    private static int count = 0;
    //自定义常量
    private static final int SHOWMOREINFO = 1000;

    private View view;

    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        //绑定布局，只不过这里是用inflate()方法
        view=inflater.inflate(R.layout.music_list,null);
        //创建listView列表并且绑定控件
        ListView listView=view.findViewById(R.id.lv);
        //实例化一个适配器
        MyBaseAdapter adapter=new MyBaseAdapter();
        //列表设置适配器
        listView.setAdapter(adapter);
        //注册快捷菜单
        registerForContextMenu(listView);
        //列表元素的点击监听器
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                //创建Intent对象，参数就是从frag1跳转到MusicActivity
                Intent intent=new Intent(FragMusicList.this.getContext(), MusicActivity.class);

                //将歌曲名和歌曲的下标存入Intent对象
                intent.putExtra("name", SongList.name[position]);
                intent.putExtra("position",String.valueOf(position));

                Bundle bundle = getArguments();
                if ( bundle != null ) {
                    //这里就拿到了之前传递的参数
                    String userAtPresentName = bundle.getString("username");

                    //写入历史记录
                    SharedPreferences shared = getActivity().getSharedPreferences(userAtPresentName + ".history", Context.MODE_PRIVATE);
                    //开始编辑
                    SharedPreferences.Editor editor = shared.edit();
                    editor.putString("history" + count, SongList.name[position]);
                    count++;
                    editor.putString("total",String.valueOf(count));
                    editor.commit();
                }
                //开始跳转
                startActivity(intent);
            }
        });
        return view;
    }

    //快捷菜单界面设计方法
    @Override
    public void onCreateContextMenu(@NonNull ContextMenu menu, @NonNull View v, @Nullable ContextMenu.ContextMenuInfo menuInfo) {
        menu.add(Menu.NONE,SHOWMOREINFO,0,"详细信息");
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    //快捷菜单动作监听方法
    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
        switch (item.getItemId()){
            case SHOWMOREINFO:{
                Intent intent = new Intent(getContext(), DetailInfoActivity.class);
                intent.putExtra("name",SongList.name[menuInfo.position]);
                startActivity(intent);
            }break;
            default:
                break;
        }
        return false;
    }

    //这里是创建一个自定义适配器，可以作为模板
    class MyBaseAdapter extends BaseAdapter{

        @Override
        public int getCount(){
            return  SongList.name.length;}
        @Override
        public Object getItem(int i){
            return SongList.name[i];}
        @Override
        public long getItemId(int i){
            return i;}

        @Override
        public View getView(int i ,View convertView, ViewGroup parent) {

            //绑定好VIew，然后绑定控件
            View view=View.inflate(FragMusicList.this.getContext(),R.layout.item_layout,null);
            TextView tv_name=view.findViewById(R.id.item_name);
            ImageView iv=view.findViewById(R.id.iv);
            //设置控件显示的内容，就是获取的歌曲名和歌手图片
            tv_name.setText(SongList.name[i]);
            iv.setImageResource(SongList.icons[i]);
            return view;
        }
    }

}